<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <!-- 引入jquery 和 common.js 可以获取到地址栏上拼接的参数 -->
    <script type="text/javascript" src="/js/common.js"></script>
    <!-- 如果到底了就提示用户到底了的消息插件-->
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script>
        $(function () {
            var user = JSON.parse(sessionStorage.getItem("user"));
            // 先从地址栏上获取当前游记的i的,发送请求获取对应的id的游记信息,渲染到页面中
            var params = getParams(); // 获取地址栏上的数据信息转成key=value的形式,调用方法
            var likeNum;
            if (params.id) {
                //根据攻略id获取大攻略信息
                $.get('/strategies/' + params.id, function (data) {
                    likeNum = data.likeNum;
                    $(".strategies").renderValues(data);
                })
                // 获取所有点赞人的id,如果其中包含当前用户的id,就让点赞的按钮转变颜色
                if (user) {
                    $.get("/strategieLikes/" + params.id, function (data) {
                        if (data.success) {
                            $(".good").addClass("fa-thumbs-up");
                            $(".good").attr("data-state", 1);
                        }
                    })
                }
                // 发送Ajax请求查询对应大攻略的分类攻略
                $.get("/strategies/" + params.id + "/catalogs", function (data) {
                    var json = {
                        list: data
                    }
                    // 数据渲染
                    var index = 1;
                    $(".catalogs").renderValues({list: data}, {
                        getName: function (item, value) {
                            var htmlStr = index + "/" + value;
                            if (index < 10) {
                                $(item).html("0" + htmlStr);
                            } else {
                                $(item).html(htmlStr);
                            }
                            index++;
                        },
                        getHref: getHref
                    });
                    // 发送Ajax请求查询对应id攻略的骡友标签
                    $.get("/strategies/" + params.id + "/tags", function (data) {
                        var json = {
                            list: data
                        }
                        // 数据渲染
                        $(".tag").renderValues(json);
                    });
                });

                //收藏
                collect();//加载页面时默认调用一次渲染收藏数目的方法
                //查询是否被收藏
                function collect() {
                    $.get("/collects/1/" + params.id, function (data) {
                        if (data) {
                            $("#collectBtn").removeClass("fa-heart-o").addClass("fa-heart");
                        } else {
                            $("#collectBtn").addClass("fa-heart-o");

                        }
                    });
                };
                //计算收藏的数量
                function countCollect() {
                    $.get("/collects/count/1/" + params.id, function (data) {
                        $(".collectBtn").renderValues({collectNum:data});
                    })
                }
                countCollect();
                $(".collectBtn").click(function () {
                    if (user) {
                        $.get("/collects/1" + "/" + params.id, function (data) {
                            if (data) {
                                // 说明用户已经收藏,提示用户,并且取消收藏
                                $.ajax({
                                    type: "DELETE",
                                    url: "/collects/1" + "/" + params.id,
                                    success: function () {
                                        $(document).dialog({
                                            type: 'notice',
                                            infoText: '亲,收藏已经取消成功!',
                                            autoClose: 1000,
                                            position: 'bottom'  // center: 居中; bottom: 底部
                                        });
                                        collect();
                                        countCollect();
                                    }
                                })
                            } else {
                                $.post("/collects/1" + "/" + params.id, function (data) {
                                    if (data.success) {
                                        $("#collectBtn").removeClass("fa-heart-o").addClass("fa-heart");
                                    }
                                    countCollect();
                                });
                            }
                        });
                    } else {
                        // alert("亲,收藏请先登录");
                        $(document).dialog({
                            type: 'notice',
                            infoText: '亲,请先登录,再收藏!',
                            autoClose: 1000,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
                    }
                });
            }
            //当前页
            var currentPage = 1;
            //总页数
            var pages;
            //存储游记的数组
            var commentArr = [];

            function query() {
                // 骡友点评查看功能
                // 获取该攻略的所有评论
                $.get("/strategies/" + params.id + "/comments", {currentPage: currentPage}, function (data) {
                    $.merge(commentArr, data.list);
                    /*{list:commentArr} 是将渲染的页面拼接到数组commentArr中*/
                    $("#comment").renderValues({list: commentArr}, {
                        // 就像渲染a标签一样的方式去渲染星星的数量
                        getStar: function (item, value) {
                            var temp = "";
                            // 实心的星星
                            for (var i = 0; i < value; i++) {
                                temp += "<i class=\"fa fa-star\"></i>"
                            }
                            // 空心的星星
                            for (var i = 0; i < (5 - value); i++) {
                                temp += "<i class=\"fa fa-star fa-star-o\"></i>"
                            }
                            $(item).html(temp);
                        },
                        getHref: getHref
                    });
                    pages = data.pages;
                })
                currentPage = currentPage + 1;
            }

            // 调用方法翻页
            query();
            //给window绑定滚动事件
            $(window).scroll(function () {
                if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                    //如果是大于总页数就不发送请求了
                    if (currentPage <= pages) {
                        query();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            content: '<span class="info-text">亲,已经到底了!</span>',
                            autoClose: 1000
                        });
                    }
                }
            });
            // 评论按钮点击事件
            $("#commentBtn").click(function () {
                if (user) {
                    window.location.href = "/mine/addComment.html?id=" + params.id;
                } else {
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '亲,你还未登陆!是否现在登陆?',
                        onClickConfirmBtn: function(){
                            window.location.href="/index.html";
                        }
                    });
                }

            });

            /*fa-thumbs-up*/
            // 点赞攻略按钮设计
            $(".good").click(function () {
                if (user) {
                    var state = $(this).data("state");
                    console.log(state);
                    if (state == 0) {
                        $(".good").addClass("fa-thumbs-up");
                        $(".good").data("state", 1);
                        $.post("/strategieLikes/" + params.id + "/like", {state: state}, function (data) {
                            if (data.success) {
                                $(document).dialog({
                                    type: 'notice',
                                    content: "<span class='info-text'>点赞成功!</span>", // 显示的文字
                                    autoClose: 1500
                                });
                                likeNum += 1
                                $(".good span").html(likeNum);
                            }
                        });
                    } else {
                        $(".good").removeClass("fa-thumbs-up");
                        $(".good").data("state", 0);
                        $.ajax({
                            url: "/strategieLikes/" + params.id, /*请求路径*/
                            type: "delete", /*修改请求方式delete*/
                            success: function () {
                                $(document).dialog({
                                    type: 'notice',
                                    content: "<span class='info-text'>取消点赞成功!</span>", // 显示的文字
                                    autoClose: 1500
                                });
                                likeNum -= 1
                                $(".good span").html(likeNum);
                            }
                        })
                    }
                } else {
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '亲,你还未登陆!是否现在登陆?',
                        onClickConfirmBtn: function(){
                            window.location.href="/index.html";
                        }
                    });
                }
            });

            //加入旅行单
            $("#userStrategy").click(function () {
                if (user) {
                    $.get("/usersProfilers/StrategyWishList/" + user.id  +"/" + params.id, function (data) {
                        console.log(data);
                        if (data) {
                            $.ajax({
                                type: "POST",
                                url: "/usersProfilers/StrategyWishList/" + user.id +"/"+ params.id,
                                success: function () {
                                    $(document).dialog({
                                        autoClose: 2000,
                                        content: '加入成功'
                                    });
                                }
                            });
                        } else {
                            $(document).dialog({
                                autoClose: 2000,
                                content: '该攻略存在您的旅行单'
                            });
                        }
                    });
                } else {
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '请登陆后再执行相关操作!',
                        onClickConfirmBtn: function () {
                            window.location.href = "/login.html"
                        }
                    });
                }
            });
        });

    </script>

</head>

<body>
<div class="search-head operationTop">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>
<div class="search-head" style="height: 48px;background: white;">
</div>
<div class="container strategies">
    <h5 class="title" render-html="title">广州攻略</h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart-o collectBtn" id="collectBtn">
            <span render-html="collectNum"></span> 收藏
        </i>
        <i class="fa fa-thumbs-o-up good" data-state="">
            &nbsp<span render-html="likeNum"> </span> 点赞
        </i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div>
                <a href="travelContent.html">
                    <button class="btn">上下九</button>
                </a>
                <a href="travelContent.html">
                    <button class="btn">入住长隆熊猫酒店，天天都是儿童节</button>
                </a>
                <a href="travelContent.html">
                    <button class="btn">飞到羊城，只为一场最地道的早茶之旅</button>
                </a>
            </div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="getName"></h6>
                    <div render-loop="list.details">
                        <a render-fun="getHref" render-key="list.details.id" data-href="/strategyDetails.html?id=">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div render-loop="list">
                <span render-html="list.name"></span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a render-fun="getHref" render-key="list.user.id"
                           data-href="/userProfiles.html?id=">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-key="list.starNum" render-fun="getStar">
                                <!--点赞的星星数量-->
                        </span>
                        <span class="comment-date" render-html="list.createTime"></span>
                        <div>
                            <a render-fun="getHref" render-key="list.id"
                               data-href="/strategyComment.html?id=">
                                <p render-html="list.content" class="commentText"></p>
                            </a>

                            <ul class="comment-img" render-loop="list.imgArr">
                                <li>
                                    <!--/upload/9d84bc42-2797-4cf7-a86f-bcf63e192c86.jpeg-->
                                    <img render-src="list.imgArr.self">
                                </li>
                            </ul>

                            <div class="d-flex icon">
                                <a render-fun="getHref" render-key="list.id"
                                   data-href="/strategyComment.html?id=">
                                    <i class="fa  fa-commenting-o " style="margin-right: 10px"> <i
                                            render-html="list.commentNums">5</i>&nbsp评论</i>
                                    <a href="#">
                                        <i class="fa fa-thumbs-o-up dian"> <span render-html="list.likeNum"></span> </i>
                                    </a>
                                </a>
                            </div>

                        </div>
                    </div>

                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn" id="userStrategy">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>


</body>
</html>